#include "array_stack.h"
#include <stdexcept>

template <class Object>
Stack<Object>::Stack(int capacity): theArray(capacity)
{
	topofStack = -1;
}

template <class Object>
bool Stack<Object>::isEmpty() const
{
	return topofStack == -1;
}

template <class Object>
bool Stack<Object>::isFull() const
{
	return topofStack == (theArray.size() -1);
}

template <class Object>
void Stack<Object>::makeEmpty()
{
	topofStack = -1;
}

template <class Object>
void Stack<Object>::push(const Object &x)
{
	if (isFull())
		throw std::range_error("stack full");
	theArray[++topofStack] = x;
}

template <class Object>
const Object & Stack<Object>::top() const
{
	if (isEmpty())
		throw std::range_error("stack empty");
	return theArray[topofStack];
}

template <class Object>
void Stack<Object>::pop()
{
	if (isEmpty())
		throw std::range_error("stack empty");
	topofStack--;
}

template <class Object>
Object Stack<Object>::topAndPop()
{
	if (isEmpty())
		throw std::range_error("stack empty");
	return theArray[topofStack--];
}
